package com.tang.window;

import com.mysql.cj.log.Log;
import com.tang.bean.WaterSensor;
import com.tang.functions.WaterSensorMapFunction;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 聚合函数
 *
 * @author tang
 * @since 2023/6/11 12:25
 */
public class WindowAggregateDemo {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        SingleOutputStreamOperator<WaterSensor> source = env
                .socketTextStream("192.168.70.141", 7777)
                .map(new WaterSensorMapFunction());

        KeyedStream<WaterSensor, String> keyedStream = source.keyBy(WaterSensor::getId);

        // 1。窗口分配器
        WindowedStream<WaterSensor, String, TimeWindow> window = keyedStream
                .window(TumblingProcessingTimeWindows.of(Time.seconds(20)));
        // 2. 窗口函数：增量聚合 Aggregate
        /* 
         * 1、属于本窗口的第一条数据来，创建窗口，创建累加器
         * 2、增量聚合： 来一条计算一条， 调用一次add方法
         * 3、窗口输出时调用一次getresult方法
         * 4、输入、中间累加器、输出 类型可以不一样，非常灵活
         */
        SingleOutputStreamOperator<String> aggregate = window.aggregate(
                /*
                 * 1-The type of the values that are aggregated (input values) 输入类型
                 * 2-The type of the accumulator (intermediate aggregate state). 累加器类型
                 * 3-The type of the aggregated result 输出类型
                 */
                new AggregateFunction<WaterSensor, Integer, String>() {
                    /**
                     * 创建累加器，给定累加器的初始值
                     * @return 累加器初始值
                     */
                    @Override
                    public Integer createAccumulator() {
                        System.out.println("创建累加器");
                        return 0;
                    }

                    /**
                     * 聚合逻辑
                     * 
                     * @param value The value to add
                     * @param accumulator The accumulator to add the value to
                     * @return 累加后的值
                     */
                    @Override
                    public Integer add(WaterSensor value, Integer accumulator) {
                        System.out.println("调用add方法,value=" + value);
                        return accumulator + value.getVc();
                    }

                    /**
                     * 窗口结束时调用的结果输出
                     * 
                     * @param accumulator The accumulator of the aggregation
                     * @return 输出的结果
                     */
                    @Override
                    public String getResult(Integer accumulator) {
                        System.out.println("调用getResult方法");
                        return accumulator.toString();
                    }

                    @Override
                    public Integer merge(Integer a, Integer b) {
                        // 只有会话窗口才会用到
                        System.out.println("调用merge方法");
                        return null;
                    }
                });

        aggregate.print();

        env.execute();

    }

}
